package com.tdkj.action.backstage.website;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;

import com.tdkj.action.BaseAction;
import com.tdkj.model.friendlylink.BeanFriendlylink;
import com.tdkj.model.shop.BeanShop;
import com.tdkj.model.user.BeanUser;
import com.tdkj.model.website.BeanComplaint;
import com.tdkj.model.website.BeanProtocol;
import com.tdkj.model.website.BeanProtocolDetail;
import com.tdkj.service.website.IBeanComplaintService;
public class ComplaintAction  extends BaseAction{
	private long id;
	private String rcontent;
	private BeanComplaint complaint;
	private List<BeanComplaint> complaints=new ArrayList<BeanComplaint>();
	private IBeanComplaintService complaintService;
	private String[] ids;
	public String deleteAll() {
		if (null != ids) {
			for (String idtemp : ids) {
				this.getBaseService().delete(Long.parseLong(idtemp), BeanComplaint.class);
			}
		}
		return findAll();
	}
	public String[] getIds() {
		return ids;
	}

	public void setIds(String[] ids) {
		this.ids = ids;
	}

	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public String getRcontent() {
		return rcontent;
	}
	public void setRcontent(String rcontent) {
		this.rcontent = rcontent;
	}
	public BeanComplaint getComplaint() {
		return complaint;
	}
	public void setComplaint(BeanComplaint complaint) {
		this.complaint = complaint;
	}
	public List<BeanComplaint> getComplaints() {
		return complaints;
	}
	
	public void setComplaints(List<BeanComplaint> complaints) {
		this.complaints = complaints;
	}
	public IBeanComplaintService getComplaintService() {
		return complaintService;
	}
	@Resource
	public void setComplaintService(IBeanComplaintService complaintService) {
		this.complaintService = complaintService;
	}

	public String findAll(){
		complaints=complaintService.findAll(BeanComplaint.class);
		if(complaints.size()==0)appendObj(20);
		Criteria criteria = this.createCriteria(BeanComplaint.class);
		criteria.addOrder(Order.desc("createtime"));// 对状态进行排序
		this.queryByPage(criteria, "complaintAction!findAll.action", 10);
		return "findAll";
	}
	
	//获取添加界面
	public String findEditPage(){
		complaint=complaintService.findById(this.getId(),BeanComplaint.class);
		return "editPage";
	}
	public String deleteComplaint(){
		complaintService.delete(this.getId(), BeanComplaint.class);
		return findAll();
	}
	
	public String replyComplaint(){
		//complaintService.delete(this.getId(), BeanComplaint.class);
		BeanComplaint bean=complaintService.findById(this.getId(),BeanComplaint.class);
		bean.setRcontent(this.getRcontent());
		bean.setRtime(new Timestamp(System.currentTimeMillis()));
		bean.setRuser(this.getCurrentUser());
		complaintService.merge(bean);
		return findAll();
	}
	

	private  void appendObj(int num) {
		// TODO Auto-generated method stub
		BeanComplaint bean=null;
		for(int i=0;i<num;i++){
			bean=new BeanComplaint();
			bean.setContent("这样不好吧！");
			BeanUser u=complaintService.findById(5, BeanUser.class);
			bean.setUser(u);
			BeanShop p=complaintService.findById(5, BeanShop.class);
			bean.setCreatetime(new Timestamp(System.currentTimeMillis()));
			complaintService.save(bean);
		}
	}
}
